/**
 * 楼栋回填列表
 */
$(function(){
	/**
	 * 点击搜索按钮
	 */
	$('div span.btn_search').click(function(){
		var data = {};
		data.communityId=$(':input[name=communityId][type=hidden]').val();
		data.buildName = $(':input[name=buildName]').val();
		$.post(ctx+"/common/build/backfill", data, function(htmlText){
			layer.closeAll(); 
			layer.open({
			        type: 1,
			        title: '请选择小区',
//			        area: ['1170px', '800px'], //宽高
			        content: htmlText 
			});
		});
	});
	
	/**
	 * 实现分页条件查询
	 */
	console.log('popBuildListTableDiv');
	$('#popBuildListTableDiv div.my-pagination > div > a').click(function(){
		var data = {};
		data.communityId=$(':input[name=communityId][type=hidden]').val();
		data.buildName = $(':input[name=buildName]').val();
		var href = $(this).attr('href');
		//截取href中#之后的值
		var str = href.substring(1,href.Length);
		data.page=parseInt(str);
		getPopTablePageData(data);
	});
	$('#popBuildListTableDiv div.my-pagination .page-current').change(function(){
		var data = {};
		data.communityId=$(':input[name=communityId][type=hidden]').val();
		data.buildName = $(':input[name=buildName]').val();
		var page = $(this).val();
		data.page=parseInt(page);
		getPopTablePageData(data);
	});
	var radioboxs = $("table.my-table-body tbody ").find(':input[type=radio]');
	console.log(JSON.stringify(buildList.data));
	alreadyChoose(radioboxs,buildList.data);
	adjustTableCellWidth('popBuildListTableDiv');
	
	/**
	 * 获取回填数据
	 */
	$('table.my-table-body > tbody > tr ').click(function(){
		var data = {};
		data.id = $(this).find(':input[type=radio]').val();
		data.name = $(this).find('td:eq(1)').html();
		buildList.backfillFun(data);
		layer.closeAll();
	});
});

function getPopTablePageData(data){
	doDataPost(data, false, ctx+"/common/build/backfill", function(msg){
		$($('.layui-layer-content')[0]).empty();
		$($('.layui-layer-content')[0]).append(msg);
		layer.iframeAuto(indexLayer);
	});
}


/**
 * 已经选择的楼栋
 */
function alreadyChoose(radioboxs,data){	
	var radios = radioboxs;
	var checkedIds = data.checkedIds;
	$(radios).each(function(){
		if(isChecked(this.value,checkedIds)){			
			$(this).attr("checked","checked");			
		}
	});
}

function isChecked(v,ids){	
	if(typeof(v) == "undefined"){
		return false;
	}else if(typeof(ids) == "undefined"){
		return false;
	}
	for(var i=0,a;a=ids[i++];){		
	  if(v == a){
		  return true;
	  }
	}
	return false;
}